<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
    <link rel="stylesheet" href="../../styles/common.css" />
    <link rel="stylesheet" href="../../styles/page.css" />
    <style>
        .card-detail {
            padding: 20px;
        }
        .detail-item {
            margin-bottom: 20px;
        }
        .detail-item label {
            font-weight: bold;
        }
        .card-image {
            max-width: 100%;
            height: auto;
            border-radius: 4px;
        }
        .el-button--text{
            /* color: red; */
            color: lightblue;
            margin-top: 15px;
            margin-left: 30px;
        }
        .el-button--text :hover{
            color: aqua;
        }

    </style>
</head>
<body>
<div id="card-detail-app">
    <!-- <el-button type="text" @click="goBack()"> 返回 </el-button> -->
    <el-card class="card-detail">
        <div class="detail-content">
            <el-row :gutter="20">
                <el-col :span="24">
                    <div class="detail-item">
                        <label>会员卡名称：</label>
                        <span>{{ cardDetail.name }}</span>
                    </div>
                </el-col>
                <el-col :span="24">
                    <div class="detail-item">
                        <label>店主名称：</label>
                        <span>{{ cardDetail.username }}</span>
                    </div>
                </el-col>
                <el-col :span="24">
                    <div class="detail-item">
                        <label>价格：</label>
                        <span>{{ cardDetail.price }} </span>
                    </div>
                </el-col>
                <el-col :span="24" v-if="cardDetail.type === '期限卡'">
                    <div class="detail-item">
                        <label>有效期：</label>
                        <span>{{ cardDetail.day }} 天</span>
                    </div>
                </el-col>
                <el-col :span="24" v-if="cardDetail.type === '计次卡'">
                    <div class="detail-item">
                        <label>使用次数：</label>
                        <span>{{ cardDetail.num }}</span>
                    </div>
                </el-col>
                <el-row :gutter="20">
                    <el-col :span="24">
                        <div class="detail-item">
                            <label>已卖出：</label>
                            <p>{{ cardDetail.sellNum }}</p>
                        </div>
                    </el-col>
                </el-row>
                <el-col :span="24">
                    <div class="detail-item">
                        <label>会员卡标签：</label>
                        <span>{{ cardDetail.tags }}</span>
                    </div>
                </el-col>
                <el-col :span="24">
                    <div class="detail-item">
                        <label>更新时间：</label>
                        <span>{{ cardDetail.updateTime }}</span>
                    </div>
                </el-col>
            </el-row>
            <el-row :gutter="20">
                <el-col :span="24">
                    <div class="detail-item">
                        <label>会员卡内容：</label>
                        <p>{{ cardDetail.remark }}</p>
                    </div>
                </el-col>
            </el-row>

            <el-row :gutter="20">
                <el-col :span="24">
                    <div class="detail-item">
                        <label>会员卡图片：</label>
                        <img :src="getImage(cardDetail.background)" alt="会员卡图片" class="card-image">
                    </div>
                </el-col>
            </el-row>
        </div>
    </el-card>

    <div>
        <el-button type="primary" @click="selectUser">查询拥有该卡的用户</el-button>
    </div>
    <el-table :data="tableData" stripe class="tableBox" >
        <el-table-column type="selection" width="25"></el-table-column>
        <el-table-column prop="username" label="用户名称"></el-table-column>
        <el-table-column prop="phone" label="电话"></el-table-column>
        <el-table-column prop="level" label="等级"></el-table-column>
        <el-table-column prop="num" label="剩余次数"></el-table-column>
        <el-table-column prop="remainingTime" label="到期时间"></el-table-column>
        </el-table-column>
    </el-table>
</div>


<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../js/validate.js"></script>
<script src="../../js/index.js"></script>
<script src="../../api/card.js"></script>
<script>
    let tagsOptions = [];
    new Vue({
        el: '#card-detail-app',
        data() {
            return {
                id:'',
                tableData : [],
                cardDetail: {
                    name: '黄金会员卡',
                    price:100,
                    username: '张三',
                    day: 365,
                    num: 100,
                    background: 'https://via.placeholder.com/300',
                    remark: '这是一张黄金会员卡，享受店内所有服务。',
                    sellNum:100,
                }
            };
        },
        computed: {
            rules () {
                return {
                    'name': [
                        {'required': true, 'message': '请填写名称', 'trigger': 'blur'}
                    ],

                    'price': [
                        {
                            'required': true,
                            validator: (rules, value, callback) => {
                                if (!value) {
                                    callback(new Error('请填写价格'))
                                } else {
                                    const reg = /^\d+(\.\d{0,2})?$/;
                                    if (reg.test(value)) {
                                        if(value < 10000){
                                            callback()
                                        }else{
                                            callback(new Error('价格小于10000'))
                                        }
                                    } else {
                                        callback(new Error('价格格式只能为数字,且最多保留两位小数'))
                                    }
                                }
                            },
                            'trigger': 'blur'
                        }
                    ],
                }
            }
        },
        created() {
            const urlParams = new URLSearchParams(window.location.search);
            this.id = urlParams.get('id');
            if (!this.id) {
                console.error('未获取到shopkeeper_id');
                // 可以添加错误处理，如重定向回原页面
            }
            this.init()
        },
        mounted() {
        },
        methods: {
            async init () {
                console.log(this.id)
                var _this = this
                // console.log(_this.tags)
                queryCardById(this.id).then(res => {
                    console.log(res);
                    if (res.code === 1) {
                        this.cardDetail = { ...res.data };
                        this.cardDetail.status = res.data.status == '1'
                        this.imageUrl = $minioUrl+res.data.background
                        // var arr = res.data.tags   //搏击,瑜伽  ->  ['搏击', '瑜伽',]
                        // var split = arr.split(",");
                        // console.log(split)
                        // this.ruleForm.tags = split
                        // console.log(arr)
                        // console.log(this.ruleForm.tags)
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                })

            },
            selectUser(){
                getByIdWithUser(this.id).then(res => {//请求结果处理
                    if (String(res.code) === '1') {
                        this.tableData = res.data
                        console.log(this.tableData)
                    }else{
                        this.$message.error(res.msg)
                    }
                })
            },
            handleCheckAllChange(val) {
                this.ruleForm.tags = val ? tagsOptions : []
                this.isIndeterminate = false;
                console.log(this.ruleForm.tags)

            },
            handleCheckedCitiesChange(value) {
                let checkedCount = value.length;
                this.checkAll = checkedCount === this.tags.length;
                this.isIndeterminate = checkedCount > 0 && checkedCount < this.tags.length;
                this.ruleForm.tags = value
                console.log(this.ruleForm.tags)
            },

            handleAvatarSuccess (response, file, fileList) {
                // 拼接down接口预览
                if(response.code === 0 && response.msg === '未登录'){
                    window.top.location.href = '../login/login.html'
                }else{
                    this.imageUrl = $minioUrl+response.data;
                    this.ruleForm.image = response.data
                }
            },
			getImage (background) {
			    return $minioUrl+background
			},

            onChange (file) {
                if(file){
                    const suffix = file.name.split('.')[1];
                    const size = file.size / 1024 / 1024 < 2;
                    if(['png','jpeg','jpg'].indexOf(suffix) < 0){
                        this.$message.error('上传图片只支持 png、jpeg、jpg 格式！');
                        this.$refs.upload.clearFiles();
                        return false
                    }
                    if(!size){
                        this.$message.error('上传文件大小不能超过 2MB!');
                        return false
                    }
                    return file
                }
            },

            goBack(){
                window.parent.menuHandle({
                    id: '6',
                    url: '/back/page/card/list.html',
                    name: '卡片模板'
                },false)
            }
        }
    })
</script>
</body>
</html>